package com.fofia.iotweb.web.controllers.api;


import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.Contants.SolonBootConfigDto;
import com.fofia.iotweb.web.domain.service.SmartTagService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import com.fofia.iotweb.framework.core.RequestHelper;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;

@Mapping("SmartTag")
@Controller
public class SmartTagController {


    @Inject
    private RequestHelper requestHelper;
    @Inject
    SmartTagService smartTagService;
    @Inject
    private SolonBootConfigDto solonBootConfigDto;
    /***
     *  历史数据按时间升序
     * @return
     */
    @Post
    @Mapping("getHistoryList")
    public ResultAdaptDto getHistoryList() {

        String tagId=requestHelper.getString("tagId","");
        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        int inTime=requestHelper.getInt("inTime",0);//数据记录是否包含历史记录，为1则全部是实时数据
        //StaticLog.info("getHistoryList:{} {} {} {},{}",tagId,page,pageSize,stime,etime);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=smartTagService.getHistoryList(tagId,gatewayId,page,pageSize,stime,etime,inTime);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /***
     *  历史数据按时间降序
     * @return
     */
    @Post
    @Mapping("getHistoryDescList")
    public ResultAdaptDto getHistoryDescList() {

        String tagId=requestHelper.getString("tagId","");
        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        int inTime=requestHelper.getInt("inTime",0);//数据记录是否包含历史记录，为1则全部是实时数据
        //StaticLog.info("getHistoryDescList:{} {} {} {},{}",tagId,page,pageSize,stime,etime);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=smartTagService.getHistoryDescList(tagId,gatewayId,page,pageSize,stime,etime,inTime);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /**
     * 分页数据，按时间降序
     * @return
     */
    @Post
    @Mapping("getPageList")
    public DataTableDto getPageList() {

        String tagId=requestHelper.getString("tagId","");
        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        int inTime=requestHelper.getInt("inTime",0);//数据记录是否包含历史记录，为1则全部是实时数据
        if(solonBootConfigDto.isApilog()) {
            StaticLog.info("getPageList:{} {} {} {},{}", tagId, page, pageSize, stime, etime);
        }
        if(StrUtil.isEmpty(tagId)){
            return JsonResult.table("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.table("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.table("请输入结束时间");
        }

        try{
            var list=smartTagService.getPageList(tagId,gatewayId,page,pageSize,stime,etime,inTime);
            return list;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.table();
    }

    /**
     *  根据时间窗口获取活跃时长
     * @param tagId
     * @param startTime
     * @param endTime
     * @param interval
     * @return
     */
    @Post
    @Mapping("getIntervalList")
    public ResultAdaptDto getIntervalList(){
        String tagId=requestHelper.getString("tagId","");
        String fieldName=requestHelper.getString("fieldName","sport_time");
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        String interval=requestHelper.getString("interval","");
        //StaticLog.info("getIntervalList:{} {} {} {}",tagId,stime,etime,interval);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            //var list=smartTagService.getIntervalList(tagId,fieldName,stime,etime,interval);
            var list=smartTagService.getIntervalListByRow(tagId,fieldName,stime,etime,interval);
            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    @Post
    @Mapping("getIntervalListAll")
    public ResultAdaptDto getIntervalListAll(){
        String tagId=requestHelper.getString("tagId","");
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        String interval=requestHelper.getString("interval","");
        //StaticLog.info("getIntervalList:{} {} {} {}",tagId,stime,etime,interval);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=smartTagService.getIntervalListAll(tagId,stime,etime,interval);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /**
     *  根据时间窗口获取活跃时长
     * @param tagId
     * @param startTime
     * @param endTime
     * @param interval
     * @return
     */
    @Post
    @Mapping("getCountByTime")
    public ResultAdaptDto getCountByTime(){
        String tagId=requestHelper.getString("tagId","");

        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        //StaticLog.info("getCountByTime:{} {} {} {}",tagId,stime,etime);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var count=smartTagService.getCountByTime(tagId,stime,etime);

            var ret= JsonResult.success();
            ret.put("count",count);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /**
     *  根据时间窗口获取活跃时长
     * @param tagId
     * @param startTime
     * @param endTime
     * @param interval
     * @return
     */
    @Post
    @Mapping("getAvgTemperature")
    public ResultAdaptDto getAvgTemperature(){
        String tagId=requestHelper.getString("tagId","");

        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        //StaticLog.info("getAvgTemperature:{} {} {} {}",tagId,stime,etime);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var avgTemperature=smartTagService.getAvgTemperature(tagId,stime,etime);

            var ret= JsonResult.success();
            ret.put("avgTemperature",avgTemperature);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /***
     *  历史数据按时间降序
     * @return
     */
    @Post
    @Mapping("getLastListByHour")
    public ResultAdaptDto getLastListByHour() {

        String tagId=requestHelper.getString("tagId","");
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=smartTagService.getLastListByHour(tagId,stime,etime);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }
}
